package ferran.tomas.ta;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import ferran.tomas.dominio.Checkpoint;
import ferran.tomas.dominio.Itinerario;
import ferran.tomas.dominio.Producto;
import ferran.tomas.dominio.User;

public class Itineraries extends Activity {
	ListView lista;
	ArrayAdapter<String> adaptador;
	ArrayList<String> datos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.itinerarios);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		datos = new ArrayList<String>();

		Itinerario it = null;
		try {
			it = ControladorItinerario.getInstancia().getItinerario(User.getInstancia().getNombre());
		} catch (NoItinerariosException ex) {
			Toast.makeText(getApplicationContext(), "Usted no tiene un itinerario asignado", Toast.LENGTH_LONG).show();
		}
		catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
		
		ArrayList<Checkpoint> cp = it.getCps();
		int i = 1;
		for (Checkpoint checkpoint : cp) {
			String pos = checkpoint.getPosicion();
			
			datos.add(i + " " + checkpoint.getFecha() + ": " + pos);
			i++;
		}

		lista = (ListView) findViewById(R.id.ListView01);
		adaptador = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, datos);

		lista.setAdapter(adaptador);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		menu.setGroupVisible(R.id.grupo1, false);
		menu.setGroupVisible(R.id.grupo3, false);
		menu.setGroupVisible(R.id.grupo4, false);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {

		case R.id.add_product:
			
			return true;
		case R.id.back:
			back();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	public void back() {
		finish();
	}


}
